using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

/*
	Code inherited from a previous version. Excuse the style!
*/
namespace SqlServerDriver
{
	public partial class MetaDataEditorForm : Form
	{
		String currentConnection;
		String fieldInfo;
		String propertyName; 

		public MetaDataEditorForm(String connStr, String table_name, String field_name, String propertyNameIn, String field_info_in)
		{
			InitializeComponent();
			this.currentConnection = connStr;
			this.fieldInfo = field_info_in;
			if (String.IsNullOrEmpty(propertyNameIn))
				this.propertyName = "MS_Description";
			else
				this.propertyName = propertyNameIn;

			// add in dbo if no schema. Otherwise update won't work later
			if (table_name.IndexOf(".") == -1) table_name = "dbo." + table_name;

			this.labelField.Text = String.Format("{0}.{1}", table_name, field_name);
			this.textInfo.Text = this.fieldInfo;
		}

		public String FieldInfo
		{
			get { return this.fieldInfo; }
		}

		private void UpdateInfo_old()
		{
			// 
			try
			{
				String source = this.labelField.Text;
				// breaking up schema.table.name
				SimpleRegex reg = new SimpleRegex(@"^\s*(.*?)\.\s*(.*?)\.(.*?)\s*$");
				if (reg.MatchText(source))  // it must do!
				{
					String schema = reg[1];
					String table = reg[2];
					String field = reg[3];
					// adjust parameter depending - probably won't work in every case. non-dbo user
					String tag = (schema.ToUpper() == "DBO") ? "user" : "schema";

					// checks whether a description is already there for add/update decision
					String sql = String.Format(
					"SELECT  objname,value FROM ::fn_listextendedproperty " +
					" ('MS_Description', '{0}', '{1}', 'table', '{2}', " +
					"   'column', '{3}') ", tag, schema, table, field);

					DataSet result;
					String connStr = this.currentConnection;
					Helper.GenericMakeDset(connStr, sql, "MetaData", out result);

					bool isAdding = true;
					if (result.Tables[0].Rows.Count >= 1)
						isAdding = false;

					if (isAdding)
					{
						sql = String.Format(
						"exec sp_addextendedproperty 'MS_Description', '{4}', " +
						"  '{0}', '{1}', 'table', '{2}', " +
						"   'column', '{3}' ", tag, schema, table, field, this.fieldInfo);
						Helper.GenericExecute(connStr, sql);
					}
					else
					{
						sql = String.Format(
						"exec sp_updateextendedproperty 'MS_Description', '{4}', " +
						"  '{0}', '{1}', 'table', '{2}', " +
						"   'column', '{3}' ", tag, schema, table, field, this.fieldInfo);
						Helper.GenericExecute(connStr, sql);
					}
				}
			}
			catch (SystemException)
			{
				// do something here.
			}
		}

		private void UpdateInfo()
		{
			// 
			try
			{
				String source = this.labelField.Text;
				// breaking up schema.table.name
				SimpleRegex reg = new SimpleRegex(@"^\s*(.*?)\.\s*(.*?)\.(.*?)\s*$");
				if (reg.MatchText(source))  // it must do!
				{
					String schema = reg[1];
					String table = reg[2];
					String column = reg[3];
					String propertyName = this.propertyName;
					String propertyValue = this.fieldInfo;

					StringBuilder sb = new StringBuilder();
					ExtendedPropertyScripting.ScriptAddTableColumnProperty(sb, schema, table, column, propertyName, propertyValue);
					String script = ExtendedPropertyScripting.MakeScript(sb, ExtendedPropertyScripting.ScriptOption.addColumn);

					String connStr = this.currentConnection;
					Helper.GenericExecute(connStr, script);
				}
			}
			catch (SystemException)
			{
				// do something here.
			}
		}


		private void BtnClose_Click(object sender, EventArgs e)
		{
			String field_info_new = this.textInfo.Text;
			if (field_info_new.CompareTo(this.fieldInfo) != 0)
			{
				this.fieldInfo = field_info_new;
				UpdateInfo();
			}
			this.DialogResult = DialogResult.OK;
		}

		private void BtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}
	}
}